package com.huawei.petapp2.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.widget.BaseListAdapter;
import com.huawei.petapp2.entity.Order;
import com.huawei.petapp2.entity.OrderItems;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单列表适配器
 */
public abstract class OrderListAdapter extends BaseListAdapter<Order, OrderListAdapter.ViewHolder> {
    private Context context;

    int mType;


    protected OrderListAdapter(int type) {
        mType = type;
        this.context=context;
    }

    protected OrderListAdapter(int type,Context context) {
        mType = type;
        this.context=context;
    }

    @Override
    protected int getItemViewLayout() {

        return R.layout.item_order;
    }

    @Override
    protected ViewHolder getItemViewHolder(View itemView) {

        return new ViewHolder(itemView);
    }

    protected abstract void onOrderCancel(Order order);

    protected abstract void onOrderPurchase(Order order);

    class ViewHolder extends BaseListAdapter.ViewHolder {

        @BindView(R.id.text_order_sn)
        TextView tvOrderSn;
        @BindView(R.id.text_order_time)
        TextView tvOrderTime;
        @BindView(R.id.text_order_name)
        TextView tvOrderName;
        @BindView(R.id.text_order_phone)
        TextView tvOrderPhone;
        @BindView(R.id.text_subtotal)
        TextView tvSubtotal;
        @BindView(R.id.button_cancel)
        Button btnCancel;
        @BindView(R.id.button_purchase)
        Button btnPurchase;
        @BindView(R.id.layout_goods)
        LinearLayout goodsLayout;

        private Order mOrder;

        ViewHolder(View itemView) {
            super(itemView);
            switch (mType) {
                case 1:
                    btnCancel.setVisibility(View.VISIBLE);
                    btnPurchase.setVisibility(View.VISIBLE);
                    break;
                case 0:
                    btnCancel.setVisibility(View.VISIBLE);
                    break;
            }
        }

        @Override
        protected void bind(int position) {
            mOrder = getItem(position);
            String orderSn = getContext().getString(R.string.order_sn, mOrder.getOrderId());
            tvOrderSn.setText(orderSn);
            String orderTime = getContext().getString(R.string.order_time, mOrder.getCreateTime());
            tvOrderTime.setText(orderTime);

            String orederName=mOrder.getReceiverName();
            tvOrderName.setText(orederName);

            String orederNumber=mOrder.getReceiverMobile();
            tvOrderPhone.setText(orederNumber);

            @SuppressLint("StringFormatMatches") String subTotal = getContext().getString(R.string.order_total, mOrder.getActualAmount());
            tvSubtotal.setText(subTotal);
            goodsLayout.removeAllViews();
            List<OrderItems> goodsList = mOrder.getOrderItems();
            for (OrderItems goods : goodsList) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.item_order_list_goods, goodsLayout, false);
                GoodsItemHolder holder = new GoodsItemHolder(view);
                holder.bind(goods);
                goodsLayout.addView(view);
            }
        }

        @OnClick({R.id.button_cancel, R.id.button_purchase}) void onClick(View view) {
            if (view.getId() == R.id.button_cancel) {
                onOrderCancel(mOrder);
            } else {
                onOrderPurchase(mOrder);
            }
        }

        class GoodsItemHolder {
            @BindView(R.id.text_goods_name)
            TextView tvName;
            @BindView(R.id.text_amount)
            TextView tvAmount;
            @BindView(R.id.image_goods)
            ImageView ivGoods;

            private OrderItems mOrderGoods;
            GoodsItemHolder(View view) {
                ButterKnife.bind(this, view);
            }
            @SuppressLint("StringFormatMatches")
            public void bind(OrderItems orderGoods) {
                mOrderGoods = orderGoods;
                tvName.setText(orderGoods.getProductName());
                int number = mOrderGoods.getTotalAmount();
                tvAmount.setText(getContext().getString(R.string.order_goods_amount, number));
                if(context!=null)
                Glide.with(context)
                        .load(mOrderGoods.getProductImg())
                        .into(ivGoods);
               // GlideUtils.loadPicture(mOrderGoods.getImg(), ivGoods);
            }
            @OnClick
            void onClick() {
                /*Intent intent = GoodDatailActivity
                        .getStartIntent(getContext(),Integer.parseInt(mOrderGoods.getProductId()) );
                getContext().startActivity(intent);*/
            }
        }
    }

}
